//获取应用实例
const app = getApp()
var city = require('../../utils/city.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    host: app.globalData.host,
    banner: '',
    category: '',
    latitude: app.globalData.latitude,
    longitude: app.globalData.longitude,
    positioName: '',
    city: '',
    is_authorize_location: app.globalData.is_authorize_location,
    topIndex: 0,
    bannerBox: false,
    orientationList: [],
    act_addList: [
      {
        id: "01", region: "优选特工",
        product:[
          {
            id:'001',
            image:"asdsdds",
            tit:'aaaaa',
            label:'asasssssssd',
            price:'42.90',
          },{

          }
        ]
      },
      {
        id: "02", region: "时令鲜果",
      },
      {
        id: "03", region: "基地茶饮",
      },
      {
        id: "04", region: "坚果果干",
      },
      { id: "05", region: "品质粗粮",},
      {
        id: "06", region: "直销瓜果",
      },
    ],
    toView: 'inToView',
    background: ['../../images/1.jpg', '../../images/2.jpg', '../../images/4.jpg'],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
  },

  scrollToViewFn: function (e) {
    var that = this;
    var idx = e.currentTarget.dataset.idx;
    var _id = e.currentTarget.dataset.id;

    this.setData({
      topIndex: e.currentTarget.dataset.idx,
      toView: 'inToView' + _id
    })
    console.log(this.data.toView)
  },
  onLoad: function() {
    var that = this;
    var data = [
      { id: "01", region: "优选特工" },
      { id: "02", region: "时令鲜果" },
      { id: "03", region: "基地茶饮" },
      { id: "04", region: "坚果果干" },
      { id: "05", region: "品质粗粮" },
      { id: "06", region: "直销瓜果" },
    ]
    that.setData({
      orientationList: data,
    })
  },
  onShow: function () {
    var that = this;
    if (app.globalData.latitude == '' || app.globalData.latitude == undefined) {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
            app.globalData.is_authorize_location = 0;
            that.setData({
              is_authorize_location: 0
            })
            wx.showModal({
              content: '佳品小栈需要您的地理位置',
              confirmText: '去开启',
              success:function(res) {
                if (res.confirm == true){
                  wx.openSetting({
                    success:function(res){
                      if (res.authSetting['scope.userLocation'] == true){
                        that.village_LBS();
                      }
                    } 
                  })
                }
              },
            })
          } else {
            that.village_LBS();
          }
        }
      }) 
    } else {
      var latitude = app.globalData.latitude;
      var longitude = app.globalData.longitude;
      that.getLocation(latitude, longitude);
      that.dingwei(latitude, longitude);
    }
  },

village_LBS: function () {
  var that = this;
  wx.getLocation({
    type: 'gcj02',
    success: function (res) {
      var latitude = res.latitude;
      var longitude = res.longitude;
      app.globalData.latitude = latitude;
      app.globalData.longitude = longitude;
      that.getLocation(latitude, longitude);
      that.dingwei(latitude, longitude);
    },
    fail: function() {
      app.globalData.is_authorize_location = 0;
      that.setData({
        is_authorize_location: 0
      })
    }
  })
},


  //定位
  dingwei: function (latitude, longitude) {
    app.globalData.is_authorize_location = 1;
    this.setData({
      is_authorize_location: 1
    })
    var that = this;
    app.wxPost(app.globalData.host + '/info/dingwei', { from: latitude + ',' + longitude }).then(res => {
      that.getCates();
      that.getBanner();
    });
  },
  //获取城市
  getLocation: function (latitude, longitude) {
    var that = this;
    app.wxPost(app.globalData.host + '/info/getLocation', { from: latitude +',' + longitude}).then(res => {
      const street_number = res.address_component.street_number;
      const street = res.address_component.street;
      const district = res.address_component.district;
      const city = res.address_component.city;
      app.globalData.city = city;
      wx.setStorage({
        key: 'near-location',
        data: res.pois,
      })
      if (app.globalData.positioName) {
        that.setData({
          positioName: app.globalData.positioName
        })
        return false
      }
      if (street_number){
        app.globalData.positioName = street_number;
        that.setData({
          positioName: street_number
        })
      } else if(street){
        app.globalData.positioName = street;
        that.setData({
          positioName: street
        })
      } else if (district) {
        app.globalData.positioName = district;
        that.setData({
          positioName: district
        })
      } else if (city) {
        app.globalData.positioName = city;
        that.setData({
          positioName: city
        })
      }
    })
  },

  //请求获取首页展示的商品分类接口
  getCates: function() {
    var that = this;
    app.wxPost(app.globalData.host + '/info/getCates').then(res => {
      that.setData({
        category: res
      })
    }, msg => {
      wx.showModal({
        content: msg,
      })
    });
  },
  //请求获取首页banner接口
  getBanner: function(){
    var that = this;
    app.wxPost(app.globalData.host + '/info/getBanner').then(res => {
      that.setData({
        banner: res
      })
    }, msg => {
      wx.showModal({
        content: msg,
      })
    });
  },

  chooseLocation: function(e) {
    wx.chooseLocation({
      success: function (res) {

      }
    })
  },
  //跳转搜索页面
  intoSearchFunc: function() {
    wx.navigateTo({
      url: '../search/search'
    })
  },
  //跳转我消息页面
  intoMessageFunc: function() {
    wx.navigateTo({
      url: 'message/message'
    })
  },
  //跳转到收货地址
  addressListFunc: function() {
    wx.navigateTo({
      url: '../mine/addressList/addressList'
    })
  },
  //跳转到搜索地址
  intoPositionFunc:function(){
    wx.navigateTo({
      url: 'position/position'
    })
  }
})
